package tree;

public class SearchInBinarySearchTree700 {
    public TreeNode searchBST(TreeNode root, int val) {
        if (root == null) {
            return null;
        }
        if (root.val == val) {
            return root;
        }
        if (val < root.val) {
            return searchBST(root.left, val);
        } else {
            return searchBST(root.right, val);
        }
    }

    public TreeNode searchBSTLooply(TreeNode root, int val) {
        TreeNode n = root;
        while (n != null) {
            if (val == n.val) {
                return n;
            } else if (val < n.val) {
                n = n.left;
            } else {
                n = n.right;
            }
        }
        return null;
    }
}
